import os
import sys

import django

# 加载Django配置和Django APP的注册。
from db_tools.data.product import row_data

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "Django电商平台.settings")
django.setup()

# 注意: 导入数据库模型，已经要先加载Django配置和注册Django app.
from app.goods.models import Goods, GoodsCategory, GoodsImage

# 依次遍历商品信息
for goods_detail in row_data:
    # 实例化商品对象;
    goods = Goods()
    goods.name = goods_detail["name"]

    # 价格处理方式: '￥232元'处理格式为 232
    goods.market_price = float(goods_detail['market_price'].replace("￥", "").replace("元", ""))
    goods.shop_price = float(goods_detail["sale_price"].replace("￥", "").replace("元", ""))

    # 商品描述处理方式
    goods.goods_brief = goods_detail["desc"] if goods_detail["desc"] else ""
    goods.goods_desc = goods_detail["goods_desc"] if goods_detail["goods_desc"] else ""

    # 讲产品轮播图片的第一张图片作为商品列表页显示的图片
    goods.goods_front_image = goods_detail["images"][0] if goods_detail["images"] else ""

    # 将商品和商品分类关联。 商品分类只存储的三级分类(可以根据三级分类找到二级分类再找到一级分类)
    category_name = goods_detail["categorys"][-1]  # 获取到三级分类的名称
    category = GoodsCategory.objects.filter(name=category_name).first()  # 根据分类,名称找到分类对象。

    if category:
        # 将商品分类对象和商品对象绑定。
        goods.category = category

    goods.save()
    print(goods)
    print("添加商品 [%s] 成功" % (goods.name))

    # 添加商品图片
    for goods_image in goods_detail["images"]:
        # 创建商品轮播图对象
        goods_image_instance = GoodsImage()
        goods_image_instance.image = goods_image
        # 将轮播图对象和商品对象绑定
        goods_image_instance.goods = goods
        goods_image_instance.save()
